package com.yuke.cloud.service.wmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.SupplierApiDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.dto.WmsPurchaseReturnOrderDto;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseReturnOrder;
import com.yuke.cloud.service.wmc.entity.WmsStockOut;
import com.yuke.cloud.service.wmc.service.WmsPurchaseReturnOrderService;
import com.yuke.cloud.service.wmc.service.WmsStockOutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/10/12.
*/
@Api(value = "WEB - WmsPurchaseReturnOrderController", tags = "处理WmsPurchaseReturnOrder相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/purchase_return_order")
public class WmsPurchaseReturnOrderController extends BaseController {
    @Resource
    private WmsPurchaseReturnOrderService wmsPurchaseReturnOrderService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private WmsStockOutService wmsStockOutService;


    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "wmsPurchaseReturnOrder", value = "该数据记录的POJO") @RequestBody WmsPurchaseReturnOrderDto wmsPurchaseReturnOrderDto) {
        WmsPurchaseReturnOrder wmsPurchaseReturnOrder = new WmsPurchaseReturnOrder();
        BeanUtils.copyProperties(wmsPurchaseReturnOrderDto,wmsPurchaseReturnOrder);
        wmsPurchaseReturnOrder.setPurchaseReturnId(generateId());

        //订单编号
        //20位长度的字符串
        java.util.UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String uuid20 = idd[0] + idd[1] + idd[2] + idd[3];
        //12位长度,相加一共生成32位的订单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmm");
        String format = simpleDateFormat.format(new Date());
        wmsPurchaseReturnOrder.setPurchaseReturnSn(format+uuid20);

        int save = wmsPurchaseReturnOrderService.save(wmsPurchaseReturnOrder);
  /*      SupplierApiDto supplierApiDto = new SupplierApiDto();
        supplierApiDto.setSupplierId(wmsPurchaseReturnOrderDto.getSupplierId()*(-1));
        supplierApiDto.setAmountPaid(wmsPurchaseReturnOrderDto.getReceivable()*(-1));
        supplierApiDto.setAmountPayable(wmsPurchaseReturnOrderDto.getReceipts());
        //修改供应商的实付应付
        Integer integer = uacUserFeignApi.updateSupplierById(supplierApiDto);
        System.out.println("supplierApiDto" + supplierApiDto);*/

        if (save>0){
            List<WmsStockOut> goodsList = wmsPurchaseReturnOrderDto.getGoodsList();
            //为每一个商品赋值出库类型为退货单出库
            for (WmsStockOut wmsStockOut : goodsList) {
                wmsStockOut.setOutType((byte) 4);
                if (wmsPurchaseReturnOrderDto.getIsDeleted().equals(1)){
                    wmsStockOut.setIsDelete(true);
                }else {
                    wmsStockOut.setIsDelete(false);
                }
                wmsStockOut.setWarehouseId(wmsPurchaseReturnOrderDto.getWarehouseId());
                wmsStockOut.setOutboundTime(new Date());
                wmsStockOut.setPurchaseReturnId(wmsPurchaseReturnOrder.getPurchaseReturnId());
            }
            //批量插入。并修改了库存商品表的库存记录
            Integer num = wmsStockOutService.stockOutPurchase(goodsList);
            return WrapMapper.ok(num);
        }


        return WrapMapper.ok(save);
    }


    //报损单
    @PostMapping("/addBreakOrder")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addBreakOrder(@ApiParam(name = "wmsPurchaseReturnOrder", value = "该数据记录的POJO") @RequestBody WmsPurchaseReturnOrderDto wmsPurchaseReturnOrderDto) {
        WmsPurchaseReturnOrder wmsPurchaseReturnOrder = new WmsPurchaseReturnOrder();
        BeanUtils.copyProperties(wmsPurchaseReturnOrderDto, wmsPurchaseReturnOrder);
        wmsPurchaseReturnOrder.setPurchaseReturnId(generateId());

        //订单编号
        //20位长度的字符串
        java.util.UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String uuid20 = idd[0] + idd[1] + idd[2] + idd[3];
        //12位长度,相加一共生成32位的订单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmm");
        String format = simpleDateFormat.format(new Date());
        wmsPurchaseReturnOrder.setPurchaseReturnSn(format+uuid20);

        int save = wmsPurchaseReturnOrderService.save(wmsPurchaseReturnOrder);
        if (save > 0) {
            List<WmsStockOut> goodsList = wmsPurchaseReturnOrderDto.getGoodsList();
            Byte type = wmsPurchaseReturnOrderDto.getType();
            //2是报损单
            if (type.equals((byte)2)) {
                //为每一个商品赋值出库类型为退货单出库
                for (WmsStockOut wmsStockOut : goodsList) {
                    if (wmsPurchaseReturnOrderDto.getIsDeleted().equals(1)){
                        wmsStockOut.setIsDelete(true);
                    }else {
                        wmsStockOut.setIsDelete(false);
                    }
                    wmsStockOut.setOutType((byte) 3);
                    wmsStockOut.setPurchaseReturnId(wmsPurchaseReturnOrder.getPurchaseReturnId());
                    wmsStockOut.setWarehouseId(wmsPurchaseReturnOrder.getWarehouseId());
                    wmsStockOut.setOutboundTime(new Date());
                }
                Integer integer = wmsStockOutService.stockOutPurchase(goodsList);
                return WrapMapper.ok(integer);
            }
        }
            return WrapMapper.ok(save);

    }
    @GetMapping("/deleteStorageByReturn")
    @ApiOperation(httpMethod = "GET", value = "根据id删除一条记录")
    public Wrapper deleteStorageByReturn(@ApiParam(name = "id", value = "记录id") @RequestParam Long Id) {
        //修改进货退货单
        WmsPurchaseReturnOrder wmsPurchaseReturnOrder = new WmsPurchaseReturnOrder();
        wmsPurchaseReturnOrder.setPurchaseReturnId(Id);
        wmsPurchaseReturnOrder.setIsDeleted((byte) 1);
        Integer update = wmsPurchaseReturnOrderService.update(wmsPurchaseReturnOrder);
        if (update>0){
            Integer num =  wmsPurchaseReturnOrderService.deleteStorageByReturn(Id);
            return WrapMapper.ok(num);
        }
        return WrapMapper.ok(update);
    }




    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        wmsPurchaseReturnOrderService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "wmsPurchaseReturnOrder", value = "需要修改的内容") @RequestBody WmsPurchaseReturnOrder wmsPurchaseReturnOrder) {
        wmsPurchaseReturnOrderService.update(wmsPurchaseReturnOrder);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<WmsPurchaseReturnOrder> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        WmsPurchaseReturnOrder wmsPurchaseReturnOrder = wmsPurchaseReturnOrderService.findById(id);
        return WrapMapper.ok(wmsPurchaseReturnOrder);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsPurchaseReturnOrder>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);

        List<WmsPurchaseReturnOrder> list = wmsPurchaseReturnOrderService.findAll();

        List<Long> supplierIds = list.stream().map(WmsPurchaseReturnOrder::getSupplierId).collect(Collectors.toList());

        List<SupplierApiDto> supplierList = uacUserFeignApi.findSupplierByIds(supplierIds);


        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/selectCondition")
    @ApiOperation(httpMethod = "POST", value = "条件查询")
    public Wrapper<PageInfo<WmsPurchaseReturnOrderDto>> selectCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestBody WmsPurchaseReturnOrderDto wmsPurchaseReturnOrderDto
                                                                        ) {
        PageHelper.startPage(page, size);
        List<WmsPurchaseReturnOrderDto> list = wmsPurchaseReturnOrderService.selectCondition(wmsPurchaseReturnOrderDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

/*    //进货退货应收款
    @PostMapping("/selectCondition")
    @ApiOperation(httpMethod = "POST", value = "条件查询")
    public Wrapper<PageInfo<WmsPurchaseReturnOrderDto>> findAllReturnMoney(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto
    ) {
        PageHelper.startPage(page, size);
        List<WmsPurchaseReturnOrderDto> list = wmsPurchaseReturnOrderService.findReturnMoney(wmsPurchaseOrderDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }*/

}
